package com.huanbao.oil.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.huanbao.oil.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TestActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.button2)
    Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);

    }

    private Handler mHandler_a1;
    private Runnable runnable_a1;
    int FLAG = 0;

    void startA1() {
        if (mHandler_a1 != null) {
            return;
        }
        mHandler_a1 = new Handler();
        //相当于定时器，每隔2s执行一次该线程
        runnable_a1 = new Runnable() {
            @Override
            public void run() {
                if (FLAG == 0) {
                    imageView.setVisibility(View.VISIBLE);
                    imageView2.setVisibility(View.GONE);
                    FLAG = 1;
                } else {
                    imageView.setVisibility(View.GONE);
                    imageView2.setVisibility(View.VISIBLE);
                    FLAG = 0;
                }
                mHandler_a1.postDelayed(this, 1000);                             //相当于定时器，每隔2s执行一次该线程
            }
        };
        mHandler_a1.post(runnable_a1);
    }

    void stopHandler() {
        if (mHandler_a1 != null) {
            mHandler_a1.removeCallbacks(runnable_a1);
            mHandler_a1 = null;
        }
    }

    @OnClick({R.id.button, R.id.button2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:
                startA1();
                break;
            case R.id.button2:
                stopHandler();
                break;
        }
    }
}
